﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SSPDemo;

/*
 * Controls:   Keyboard    360
 * Move Right   ->          ->
 * Moonwalk     <-          <-
 * Jump         Space       A
 * Attack       Ctrl        X
 */

namespace SSP
{

    enum PlayerAction
    {
        Nothing,
        MoveRight,
        MoveLeft,
        Jump,
        Attack,
        Up,
        Down
    }

    class SSPInput
    {
        int mode; // 0 = Keyboard, 1 = 360 controller
        KeyboardState oldKBState;
        GamePadState oldGPState;
        SSPScene level;


        public SSPInput()
        {
            mode = 0;
            oldKBState = Keyboard.GetState();
            oldGPState = GamePad.GetState(PlayerIndex.One);
            //level = new PlayableLevel();
        }

        
        public SSPInput(SSPScene pl)
        {
            mode = 0;
            oldKBState = Keyboard.GetState();
            oldGPState = GamePad.GetState(PlayerIndex.One);
            level = pl;
        }
        

        public void SetMode(int new_mode)
        {
            mode = new_mode;
        }

        public void Update()
        {
            if (mode == 0)
            {
                UpdateKeyboardInput(); 
            }
            else
            {
                Update360Input();
            }
        }


        private void UpdateKeyboardInput()
        {
            KeyboardState newKBState = Keyboard.GetState();
            PlayerAction action = PlayerAction.Nothing;

            // Is the SPACE key down?
            if (newKBState.IsKeyDown(Keys.Space))
            {
                // If not down last update, key has just been pressed.
                if (!oldKBState.IsKeyDown(Keys.Space))
                {
                    action = PlayerAction.Jump;
                }
            }
            else if (newKBState.IsKeyDown(Keys.LeftControl))
            {
                action = PlayerAction.Attack;
            }
            else if (newKBState.IsKeyDown(Keys.Right))
            {
                action = PlayerAction.MoveRight;
            }
            else if (newKBState.IsKeyDown(Keys.Left))
            {
                action = PlayerAction.MoveLeft;
            }
            else if (newKBState.IsKeyDown(Keys.Up))
            {

                if (!oldKBState.IsKeyDown(Keys.Up))
                {
                    action = PlayerAction.Up;
                }

            }
            else if (newKBState.IsKeyDown(Keys.Down))
            {

                if (!oldKBState.IsKeyDown(Keys.Down))
                {
                    action = PlayerAction.Down;
                }
            }
            
            // Update saved state.
            oldKBState = newKBState;

            level.DoPlayerAction(action);
        }

        private void Update360Input()
        {
            // Get the current gamepad state.
            GamePadState newGPState = GamePad.GetState(PlayerIndex.One);
            PlayerAction action = PlayerAction.Nothing;

            // Process input only if connected and button A is pressed.
            if (newGPState.IsConnected)
            {
                if (newGPState.Buttons.A == ButtonState.Pressed)
                {
                    if (!(oldGPState.Buttons.A == ButtonState.Pressed))
                    {
                        action = PlayerAction.Jump;
                    }
                }
                else if (newGPState.Buttons.X == ButtonState.Pressed)
                {
                    action = PlayerAction.Attack;
                }
                else if (newGPState.DPad.Left == ButtonState.Pressed)
                {
                    action = PlayerAction.MoveLeft;
                }
                else if (newGPState.DPad.Right == ButtonState.Pressed)
                {
                    action = PlayerAction.MoveRight;
                }
                else if (newGPState.DPad.Up == ButtonState.Pressed)
                {
                    action = PlayerAction.Up;
                }
                else if (newGPState.DPad.Down == ButtonState.Pressed)
                {
                    action = PlayerAction.Down;
                }
            }

            oldGPState = newGPState;

            level.DoPlayerAction(action);
        }
    }
}
